package com.xindaoapp.happypet.db.model;

import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.xindaoapp.happypet.bean.PostInfo3;
import com.xindaoapp.happypet.db.DaoSession;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.KeyValuePair;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

/**
 * 保存帖子
 * 
 * @author chenghao
 * 
 */
public class PostDao extends AbstractDao<PostInfo3, String> {
    public static final String TABLENAME = "post";

    public PostDao(DaoConfig config) {
        super(config);
    }

    public PostDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /**
     * Properties of entity Note.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        /** 俱乐部fid */
        public final static Property FID =
                new Property(0, String.class, "fid", false, "fid", false);
        /** 标题 */
        public final static Property TITLE = new Property(1, String.class, "title", false, "title");
        /** 正文内容 */
        public final static Property CONTENT = new Property(2, String.class, "content", false,
                "content", false);
        /** 已发送成功的数组 */
        public final static Property SUCCESSARRAY = new Property(3, String.class, "success_array",
                false, "successarray", false);
        /** 正在发送的数组 */
        public final static Property SENDINGARRAY = new Property(4, String.class, "sending_array",
                false, "sendingarray", false);
        /** 视频封面 */
        public final static Property VIDEOCOVER = new Property(5, String.class, "video_cover",
                false, "video_cover", false);
        /** 发帖时间 */
        public final static Property POSTTIME = new Property(6, Long.class, "post_time", false,
                "post_time", false);
        public final static Property TID =
                new Property(7, String.class, "tid", false, "tid", false);
        public final static Property PID =
                new Property(8, String.class, "pid", false, "pid", false);
        public final static Property WEBVIDEODATA = new Property(9, String.class, "webvideodata",
                false, "webvideodata", false);
    };

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists ? "IF NOT EXISTS " : "";
        db.execSQL(String.format("CREATE TABLE %s '%s' (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);",
                constraint, TABLENAME, createSqlCreate(Properties.FID),
                createSqlCreate(Properties.TITLE), createSqlCreate(Properties.CONTENT),
                createSqlCreate(Properties.SUCCESSARRAY), createSqlCreate(Properties.SENDINGARRAY),
                createSqlCreate(Properties.VIDEOCOVER), createSqlCreate(Properties.POSTTIME),
                createSqlCreate(Properties.TID), createSqlCreate(Properties.PID),
                createSqlCreate(Properties.WEBVIDEODATA)));
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'" + TABLENAME + "'";
        db.execSQL(sql);
    }

    @Override
    protected PostInfo3 readEntity(Cursor cursor, int offset) {
        PostInfo3 entity = new PostInfo3();
        entity.fid = cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
        entity.title = cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1);
        entity.content = cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2);
        entity.successArray =
                (KeyValuePair<List<KeyValuePair<String, String>>, List<KeyValuePair<String, String>>>) CommonUtil
                        .objToString(cursor.isNull(offset + 3) ? null : cursor
                                .getString(offset + 3));
        entity.sendingArray =
                (List<KeyValuePair<String, Integer>>) CommonUtil.objToString(cursor
                        .isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.videoCover = cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5);
        entity.postTime = cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6);
        entity.tid = cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7);
        entity.pid = cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8);
        entity.webvideodata = cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9);
        return entity;
    }

    @Override
    protected String readKey(Cursor cursor, int offset) {
        return null;
    }

    @Override
    protected void readEntity(Cursor cursor, PostInfo3 entity, int offset) {
        entity.fid = cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
        entity.title = cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1);
        entity.content = cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2);
        entity.successArray =
                (KeyValuePair<List<KeyValuePair<String, String>>, List<KeyValuePair<String, String>>>) CommonUtil
                        .objToString(cursor.isNull(offset + 3) ? null : cursor
                                .getString(offset + 3));
        entity.sendingArray =
                (List<KeyValuePair<String, Integer>>) CommonUtil.objToString(cursor
                        .isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.videoCover = cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5);
        entity.postTime = cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6);
        entity.tid = cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7);
        entity.pid = cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8);
        entity.webvideodata = cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9);
    }

    @Override
    protected void bindValues(SQLiteStatement stmt, PostInfo3 entity) {
        stmt.clearBindings();
        stmt.bindString(1, entity.fid);
        stmt.bindString(2, entity.title);
        stmt.bindString(3, entity.content);
        stmt.bindString(4, CommonUtil.stringToObj(entity.successArray));
        stmt.bindString(5, CommonUtil.stringToObj(entity.sendingArray));
        stmt.bindString(6, entity.videoCover);
        stmt.bindLong(7, entity.postTime);
        stmt.bindString(8, entity.tid);
        stmt.bindString(9, entity.pid);
        stmt.bindString(10, entity.webvideodata);
    }

    @Override
    protected String updateKeyAfterInsert(PostInfo3 entity, long rowId) {
        return entity.title;
    }

    @Override
    protected String getKey(PostInfo3 entity) {
        if (entity != null) {
            return entity.title;
        } else {
            return null;
        }
    }

    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }

}
